package com.woniuxy.service.impl;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.woniuxy.entity.Area;
import com.woniuxy.mapper.AreaMapper;
import com.woniuxy.service.AreaService;
import com.woniuxy.utils.KeyUtil;
import com.woniuxy.utils.RedisUtil;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dto.PageDto;
import com.woniuxy.entity.Area;
import com.woniuxy.mapper.AreaMapper;
import com.woniuxy.service.AreaService;

import lombok.Synchronized;

@Service
public class AreaServiceImpl implements AreaService{
	@Autowired
	private AreaMapper areaMapper;
	@Autowired
	private RedisTemplate<String, Object> redisTemplate;


	@Override
	public PageDto<Area> findAllArea(int page){
		PageDto<Area> pageDto;
		synchronized (AreaServiceImpl.class) {
			PageHelper.startPage(page, 10);
			List<Area> areas = areaMapper.findAllArea();
			PageInfo<Area> pageInfo = new PageInfo<>(areas);
			pageDto = new PageDto<Area>()
						.setCurrentPage(page)
						.setPageSize(pageInfo.getPageSize())
						.setTotal(pageInfo.getPageSize())
						.setTotalPages(pageInfo.getPages())
						.setData(areas);
		}		
		return pageDto;	
	}

	@Override
	public int addArea(Area area) {
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String resDate = dateFormat.format(date);
		System.out.println(resDate);
		area.setAtime(resDate);
		return areaMapper.addArea(area);
	}

	@Override
	public PageDto<Area> findAreaByStatus(int status,int page) {
		PageDto<Area> pageDto;
		synchronized (AreaServiceImpl.class) {
			PageHelper.startPage(page, 10);
			List<Area> areas = areaMapper.findAreaByStatus(status);
			PageInfo<Area> pageInfo = new PageInfo<>(areas);
			pageDto = new PageDto<Area>()
						.setCurrentPage(page)
						.setPageSize(pageInfo.getPageSize())
						.setTotal(pageInfo.getPageSize())
						.setTotalPages(pageInfo.getPages())
						.setData(areas);
		}
		return pageDto;
	}

	//模糊查询
	@Override
	public PageDto<Area> findAreaByAname(String name,int page) {
		PageDto<Area> pageDto;
		synchronized (AreaServiceImpl.class) {
			PageHelper.startPage(page, 10);
			List<Area> areas = areaMapper.findAreaByAname(name);
			PageInfo<Area> pageInfo = new PageInfo<>(areas);
			pageDto = new PageDto<Area>()
						.setCurrentPage(page)
						.setPageSize(pageInfo.getPageSize())
						.setTotal(pageInfo.getPageSize())
						.setTotalPages(pageInfo.getPages())
						.setData(areas);
		}
		return pageDto;
	}

	//删除
	@Override
	public int deleteAreaByAid(int aid) {		
		return areaMapper.delectAreaByaid(aid);
	}

	//更改switch状态
	@Override
	public int updateSwitch(int aid, int value) {
		int data;
		synchronized(AreaServiceImpl.class) {
			Area area = new Area().setAid(aid).setStatus(value);
			data = areaMapper.updateSwitch(area);
		}
		return data;		
	}

	
	@Override
	public List<Area> findArea() {
		String key=KeyUtil.generate(this, "findArea");
		//System.out.println(key);
		if (redisTemplate.hasKey(key)) {
			List<Object> objects=redisTemplate.opsForList().range(key, 0, redisTemplate.opsForList().size(key));
			List<Area> areas=new ArrayList<Area>();
			ArrayList<Object> data=(ArrayList<Object>) objects.get(0);
			for (Object object : data) {
				areas.add((Area)object);
			}
			return areas;
		}else{
			List<Area> areas=areaMapper.findArea();
			redisTemplate.opsForList().leftPushAll(key, areas);
			redisTemplate.expire(key, 1, TimeUnit.DAYS);
			return areas;
		}
	}


}
